package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.*;
import com.tjetc.entity.*;
import com.tjetc.service.FoodRecordService;
import com.tjetc.service.GoodsRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Service
public class GoodsRecordServiceImpl implements GoodsRecordService {
    @Autowired
    private GoodsRecordMapper goodsRecordMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public JsonResult findById(Long id) {
        GoodsRecord goodsRecord=goodsRecordMapper.selectById(id);
        GoodsInfo goodsInfo=goodsInfoMapper.selectById(goodsRecord.getMaterialId());
        Employee employee=employeeMapper.selectById(goodsRecord.getManagerId());
        goodsRecord.setGoodsInfo(goodsInfo);
        goodsRecord.setEmployee(employee);
        return JsonResult.success(goodsRecord);
    }
    @Override
    public   JsonResult findGoodsPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<GoodsRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<GoodsRecord>goodsRecordPage= goodsRecordMapper.selectPageLikeGoodsInfo(page,name);
        for (GoodsRecord goodsRecord : goodsRecordPage.getRecords()) {
            GoodsInfo goodsInfo=goodsInfoMapper.selectById(goodsRecord.getMaterialId());
            Employee employee=employeeMapper.selectById(goodsRecord.getManagerId());
            goodsRecord.setGoodsInfo(goodsInfo);
            goodsRecord.setEmployee(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(goodsRecordPage);
    }
    @Override
    public   JsonResult findEmployeePage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<GoodsRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<GoodsRecord>goodsRecordPage= goodsRecordMapper.selectPageLikeEmployee(page,name);
        for (GoodsRecord goodsRecord : goodsRecordPage.getRecords()) {
            GoodsInfo goodsInfo=goodsInfoMapper.selectById(goodsRecord.getMaterialId());
            Employee employee=employeeMapper.selectById(goodsRecord.getManagerId());
            goodsRecord.setGoodsInfo(goodsInfo);
            goodsRecord.setEmployee(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(goodsRecordPage);
        }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=goodsRecordMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(GoodsRecord goodsRecordParam) {
        GoodsRecord goodsRecord=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(goodsRecordParam.getOperationId()==null||goodsRecordParam.getOperationId()<=0){
            //新增
            goodsRecord=new GoodsRecord();
            BeanUtils.copyProperties(goodsRecordParam,goodsRecord);
            goodsRecord.setCreateTime(now);
            goodsRecord.setUpdateTime(now);
            goodsRecordMapper.insert(goodsRecord);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            goodsRecord=goodsRecordMapper.selectById(goodsRecordParam.getOperationId());
            if(goodsRecord==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            goodsRecord.setUpdateTime(now);
            goodsRecord.setGoodsInfo(goodsRecordParam.getGoodsInfo());
            goodsRecord.setEmployee(goodsRecordParam.getEmployee());
            goodsRecord.setAfterQuantity(goodsRecordParam.getAfterQuantity());
            goodsRecord.setBeforeQuantity(goodsRecordParam.getBeforeQuantity());
            goodsRecord.setIsDeleted(goodsRecordParam.getIsDeleted());
            goodsRecord.setManagerId(goodsRecordParam.getManagerId());
            goodsRecord.setMaterialId(goodsRecordParam.getMaterialId());
            goodsRecord.setOperationTime(goodsRecordParam.getOperationTime());
            goodsRecord.setPrice(goodsRecordParam.getPrice());
            goodsRecord.setQuantity(goodsRecordParam.getQuantity());
            goodsRecord.setRemark(goodsRecordParam.getRemark());
            goodsRecord.setSupplier(goodsRecordParam.getSupplier());
            goodsRecord.setTotalAmount(goodsRecordParam.getTotalAmount());
            goodsRecord.setType(goodsRecordParam.getType());
            int rows=goodsRecordMapper.updateById(goodsRecord);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
